package com.zhongge.operations;

import com.zhongge.book.Book;
import com.zhongge.book.BookList;

import java.util.Scanner;

@SuppressWarnings("all")
/**
 * @ClassName DeleteBook
 * @Description TODO 删除图书
 * @Author zhongge
 * @Date 2025/7/14 19:30
 * @Version 1.0
 */
public class DeleteBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除的图书名字：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int tmp = -1; //被删除的元素的下标
        //判断图书是否存在
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBook(i);
            if (name.equals(book.getName())) {
               tmp = i;
            }
        }
        if (tmp == -1) {//没找到
            System.out.println("删除失败，没有" + name + "这本书");
            return;
        } else {
            //如果程序可以到达这里，那么就进行删除
            for (int i = tmp; i < bookList.getUsedSize() - 1; i++) {
                bookList.getBookArr()[i] = bookList.getBookArr()[i + 1];
            }
            bookList.getBookArr()[bookList.getUsedSize() - 1] = null;
            bookList.setUsedSize(bookList.getUsedSize() - 1);
            System.out.println("删除成功！！！");

        }
    }
}
